//
//  FollowViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-6-27.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "FollowViewController.h"
#import "TimeLineListHeaderView.h"
#import "TimeLineCell.h"
#import "UIButton+Property.h"
#import "XYAlertViewHeader.h"
#import "SheduleService.h"
#import "GameService.h"
#import "WeatherService.h"
#import "AppDelegate.h"

@interface FollowViewController ()

@end

@implementation FollowViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.eventListViewOffset = 0;
   
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([theApp isAppReachable]) {
        [self updateMainDataAndViewByDay:[AppUtils getCurDayInYogRange] isFirstLoad:YES];
    }
    [self updateDateMarks];
    
}

-(void)updateDateMarks
{
   
    self.dateMarks = [[[[SheduleService alloc] init] getAllStaredDays] allObjects];
    [UIView animateWithDuration:0.1 animations:^(){
        //
    }completion:^(BOOL finished){
        [self reloadCalendarData];
    }];
    
    
}
- (void)updateMainDataAndViewByDay:(NSInteger)day  isFirstLoad:(BOOL)first
{
  self.HUD.mode = MBProgressHUDModeIndeterminate;
    self.HUD.labelText = nil;
    [self.HUD show:YES];
    
    NSString *date = [NSString stringWithFormat:@"201408%i",day];
    
    [[[SheduleService alloc] init] getScheduleItemsByDate:day
                                               completion:^(id result, NSError *error) {
                                                   [self.HUD hide:YES];
                                                   if (error == nil && [result count] != 0) {
                                                       self.timeline = result;
                                                       self.keys = [[self.timeline allKeys] sortedArrayUsingSelector:@selector(compare:)];

                                                   }else{
                                                       self.timeline = nil;
                                                       self.keys = nil;
                                                   }
                                                   [self reloadEventData];
                                                   if (first) {
                                                       [self initSelectedDay];
                                                   }
                                               }];
    
    //天气
    [[[WeatherService alloc] init] getWeather:date
                                   completion:^(id result, NSError *error) {
                                       if (error == nil) {
                                           [self.weatherLb setText:result];
                                           NSLog(@"result is %@",result);
                                       }else{
                                           [self.weatherLb setText:@""];
                                       }
                                   }];
}


- (IBAction)followAction:(id)sender
{
    UIButton *btn = sender;
    GameEvent *event = (GameEvent*)[btn.property valueForKey:@"event"];
    //NSIndexPath *indexPath = (NSIndexPath*)[btn.property valueForKey:@"indexPath"];
    
    XYAlertView *alertView = [XYAlertView alertViewWithTitle:NSLocalizedString(@"删除关注", nil)
                                                     message:NSLocalizedString(@"确认删除该项关注", nil)
                                                     buttons:[NSArray arrayWithObjects:NSLocalizedString(@"删除", nil), NSLocalizedString(@"取消", nil), nil]
                                                afterDismiss:^(int buttonIndex) {
                                                    if (buttonIndex == 0) {
                                                        [[[SheduleService alloc] init] delScheduleItem:[NSNumber numberWithInteger:[event.id integerValue]]];
//                                                        [self.eventListView beginUpdates];
//                                                        [self.eventListView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
//                                                                              withRowAnimation:UITableViewRowAnimationAutomatic];
//                                                        [self.eventListView endUpdates];
                                                        [self updateDateMarks];
                                                        [self updateMainDataAndViewByDay:[[self.dayArr objectAtIndex:self.selectedDayIndex] integerValue] isFirstLoad:NO];
                                                    }
                                                    
                                                }];

    [alertView setButtonStyle:XYButtonStyleGray atIndex:1];

    [alertView show];
    
}

#pragma mark - collectionView delegate & datasource

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [super collectionView:collectionView didSelectItemAtIndexPath:indexPath];
    if ([theApp isAppReachable]) {
        [self updateMainDataAndViewByDay:[[self.dayArr objectAtIndex:indexPath.row] integerValue] isFirstLoad:NO];
    }
}


#pragma mark - TableView delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.timeline allKeys].count;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    NSString* key = self.keys[section];
    NSArray* nameSection = self.timeline[key];
    return nameSection.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TimeLineCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeLineCell" forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        [cell.line setHidden:YES];
    }
    
    NSString* key = self.keys[indexPath.section];
    NSArray* events = self.timeline[key];
    GameEvent* event = events[indexPath.row];
    
    //
    cell.titleLabel.text = event.title;
    cell.locationLabel.text = [NSString stringWithFormat:@"at.%@", event.place];
    cell.icon.image = event.icon;
    
    //通过setProperty给按钮传递数据
    NSDictionary *property = [[NSDictionary alloc] initWithObjectsAndKeys:event,@"event",indexPath,@"indexPath", nil];
    [cell.followBtn setProperty:property];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSArray *xibContents = [[NSBundle mainBundle] loadNibNamed:@"TimeLineListHeaderView" owner:self options:nil];
    TimeLineListHeaderView *customView = [xibContents lastObject]; //safer than objectAtIndex:0
    
    [customView.timeLabel setText:self.keys[section]];
    return customView;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.0;
}


@end
